
import Result from "../domain/vo/Result.js"
import { UserServiceImpl } from "../service/UserService.js"

const userService = new UserServiceImpl()

const getUserInfo = (userId) => {
    return Result.success(userService.getUserInfoById(userId))
}


const updateUserInfo = (userId, name, signature, email, gender) => {
    userService.updateUserInfo(userId, name, signature, email, gender)
    return Result.success()
}


const login = (username, password) => {
    return Result.success(userService.login(username, password))
}

const elogin = (email, code) => {
    return Result.error("抱歉，邮箱登录暂未开启!")
}

const registry = (username, password, email) => {
    if (!username || !password || !email) {
        throw new Error("任意信息不得为空!")
    }else if (email.indexOf("@") == -1) {
        throw new Error("无效的邮箱")
    }
    userService.registry(username, password, email)
    return Result.success()
}





export const buildController = () => {

    return {
        "/user/info": getUserInfo,
        "/user/update": updateUserInfo,
        "/user/login": login,
        "/user/registry": registry,
        "/user/elogin": elogin
    }

}